<?php

include_once("../dao/conexao_class.php");
include_once("../model/campus_model_class.php");

class CampusDAO {

    private $insert = "INSERT INTO campus(log_id, cam_nome,cam_num) values(?, ?, ?);";
    private $update = "UPDATE campus SET log_id = ?, cam_nome, cam_num = ? WHERE cam_id = ?;";
    private $delete = "DELETE FROM campus WHERE cam_id = ?;";
    private $select = "SELECT * FROM campus;";
    private $select_by_id = "SELECT * FROM campus WHERE cam_id = ?;";
    private $select_by_nome = "SELECT * FROM campus WHERE cam_nome = ?;";

    function insert($objCampus) {
        //conectando ao banco
        $objConn = new Conexao();
        $conn = $objConn->conecta();
        
        //criando o preparedStatment
        
        if ($stm = $conn->prepare($this->insert)) {

            $stm->bind_param("isi", $objCampus->getLogradouro(), $objCampus->getNome(), $objCampus->getNumero());
            
            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        } else {
            return false;
        }
    }

    function update($objCampus) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->update)) {

            $stm->bind_param("isii", $objCampus->getLogradouro(), $objCampus->getNome(), $objCampus->getNumero(), $objCampus->getId());

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }

    function delete($id) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        
        if ($stm = $conn->prepare($this->delete)) {

            $stm->bind_param("i", $id);

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }
    
    
    function findAllCampusJSON() {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->select)) {

            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $logradouro, $nome, $numero);


            //para cada resultado encontrado
            
            $lista = array();
            while ($stm->fetch()) {
//                echo "Id: ".$id." Descrição: ".$descricao."<br>";
                $lista['campus'][] = array(
                    'id' => $id,
                    'logradouro' => $logradouro,
                    'nome' => $nome,
                    'numero' => $numero
                );
            }

            $conn->close();
            return $lista;
        }
    }



    function findAllCampus() {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->select)) {

            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $logradouro, $nome, $numero);


            //para cada resultado encontrado
            $i = 0;
            $lista = array();
            
            while ($stm->fetch()) {

                $campus = new CampusModel();
                
                $campus->setId($id);
                $campus->setLogradouro($logradouro);
                $campus->setNome($nome);
                $campus->setNumero($numero);
                
                $lista[] = $campus;
            }
            
            $conn->close();
            return $lista;
        }
    }
    
    function findCampusById($campus) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $campM = new CampusModel();

        if ($stm = $conn->prepare($this->select_by_id)) {
            
            $stm->bind_param("i", $campus);
            
            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $logradouro, $nome, $numero);


            //para cada resultado encontrado
            
            
            while ($stm->fetch()) {
                $campM = new CampusModel();
                $campM->setId($id);
                $campM->setLogradouro($logradouro);
                $campM->setNome($nome);
                $campM->setNumero($numero);
                
            }

            $conn->close();
            return $campM;
        }
    }
    
    function existe($nome) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->select_by_nome)) {
            
            $stm->bind_param('s', $nome);
            
            $stm->execute();

            while ($stm->fetch()) {
                return true;
            }
            
            $conn->close();
            return false;
        }
    }
    
}
//$teste = new CampusDAO();
//
//$cam = new CampusModel();
//$cam->setLogradouro(608514);
//$cam->setNome("Engenharia");
//$cam->setNumero(309);


//if($teste->insert($cam)){
//    printf("Sucesso!");
//}else{
//    printf("Erro!");
//}
//
//var_dump($teste->findAllCampus());

